package com.fhnew.app.ui.activity.recomend.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResCommission;
import com.fhnew.app.model.network.entity.req.ResRecode;

import com.fhnew.app.util.DoubleDatePickerDialog;
import com.fhnew.app.util.EventBus.ReloginEvent;
import com.fhnew.app.widget.MyRecyclerView;
import com.fhnew.app.util.OnEndLessScrollListener;
import com.fhnew.app.util.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/9/25.
 * 我的佣金
 */
public class MycommissionFragment extends Fragment {

    private LinearLayout commislayout;
    private TextView mycommissionstarttime;
    private TextView mycommissionendtime;
    private TextView mycommissionsearch;
    private TextView mycommissionclear;
    private MyRecyclerView mycommissionlist;
    private TextView mycommissionmoney;
    private TextView mycommissionusernumber;

    private TextView mycommissionhistrynumber;
    private TextView mycommissionhistrymoney;
    private String starttime;
    private String endtime;
    private View mHeaderView;
    private ArrayList<ResCommission.DataBean> dataBeans = new ArrayList<>();
    private Adapter adapter;

    private int page;//初始页数

    private OnEndLessScrollListener onEndLessScrollListener;


    private void initView (View view) {
        commislayout = view.findViewById(R.id.commislayout);
        mycommissionstarttime = view.findViewById(R.id.mycommissionstarttime);
        mycommissionendtime = view.findViewById(R.id.mycommissionendtime);
        mycommissionsearch = view.findViewById(R.id.mycommissionsearch);
        mycommissionclear = view.findViewById(R.id.mycommissionclear);
        mycommissionlist = view.findViewById(R.id.mycommissionlist);
        mycommissionmoney = view.findViewById(R.id.mycommissionmoney);
        mycommissionusernumber = view.findViewById(R.id.mycommissionusernumber);

        mycommissionstarttime.setOnClickListener(onClickListener);
        mycommissionendtime.setOnClickListener(onClickListener);
        mycommissionsearch.setOnClickListener(onClickListener);
        mycommissionclear.setOnClickListener(onClickListener);
    }

    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.mycommissionlayout, null);
        initView(container);
        return container;
    }

    @Override
    public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //初始化控件
        mycommissionlist.setNestedScrollingEnabled(false);
        page = 0;


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        onEndLessScrollListener = new OnEndLessScrollListener(linearLayoutManager) {
            @Override
            public void getMoreData (int currentPage) {
                //下拉加载的时候页数加1
                searchcommission(++page);
            }
        };
        mycommissionlist.setLayoutManager(linearLayoutManager);


        mHeaderView = View.inflate(getContext(), R.layout.mycommissionlayoutheader, null);
        //初始化头部布局
        InitHeaderView();
        mycommissionlist.addHeaderView(mHeaderView);

        adapter = new Adapter();
        mycommissionlist.setAdapter(adapter);
        mycommissionlist.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));


        //添加滑动监听
        mycommissionlist.addOnScrollListener(onEndLessScrollListener);
    }

    @Override
    public void onHiddenChanged (boolean hidden) {
        super.onHiddenChanged(hidden);
        page = 0;
        searchcommission(page);
    }

    private void InitHeaderView () {
        mycommissionhistrynumber = (TextView) mHeaderView.findViewById(R.id.mycommissionhistrynumber);
        mycommissionhistrymoney = (TextView) mHeaderView.findViewById(R.id.mycommissionhistrymoney);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.mycommissionstarttime:
                    Calendar c = Calendar.getInstance();
                    new DoubleDatePickerDialog(getContext(), 0, new DoubleDatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet (DatePicker startDatePicker, int startYear, int startMonthOfYear,
                                               int startDayOfMonth, DatePicker endDatePicker, int endYear, int endMonthOfYear,
                                               int endDayOfMonth) {
                            mycommissionstarttime.setText("开始时间：" + startYear + "-" + (startMonthOfYear + 1) + "-" + startDayOfMonth);
                            starttime = startYear + "-" + (startMonthOfYear + 1) + "-" + startDayOfMonth;
                        }
                    }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), false).show();
                    break;
                case R.id.mycommissionendtime:
                    Calendar c2 = Calendar.getInstance();
                    new DoubleDatePickerDialog(getContext(), 0, new DoubleDatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet (DatePicker startDatePicker, int startYear, int startMonthOfYear,
                                               int startDayOfMonth, DatePicker endDatePicker, int endYear, int endMonthOfYear,
                                               int endDayOfMonth) {
                            mycommissionendtime.setText("结束时间：" + startYear + "-" + (startMonthOfYear + 1) + "-" + startDayOfMonth);
                            endtime = startYear + "-" + (startMonthOfYear + 1) + "-" + startDayOfMonth;
                        }
                    }, c2.get(Calendar.YEAR), c2.get(Calendar.MONTH), c2.get(Calendar.DATE), false).show();
                    break;
                case R.id.mycommissionsearch:
                    searchcommission(page);
                    break;
                case R.id.mycommissionclear:
                    starttime = "";
                    endtime = "";
                    mycommissionstarttime.setText("开始时间");
                    mycommissionendtime.setText("结束时间");
                    break;
            }
        }
    };


    private void searchcommission (int page) {
        HttpJsonUtils.getHttpApiInstance()
                .getcommission(Constants.getuseAdress() + "api/m/promotion/commission", page + "", "10", starttime, endtime, UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<ResCommission>(MycommissionFragment.this) {
                .subscribe(new CustomObserver<ResCommission>() {
                    @Override
                    protected void onCustomNext (ResCommission resCommission) {
                        if (resCommission.code == 0) {
                            dataBeans.clear();
                            dataBeans.addAll(resCommission.data);
                            adapter.notifyDataSetChanged();
                            mycommissionhistrynumber.setText("历史笔数: " + resCommission.otherData.historyCount);
                            mycommissionhistrymoney.setText("历史总佣金: " + resCommission.otherData.historyMoney);
                            mycommissionmoney.setText("" + resCommission.otherData.historyMoney);
//                            mycommissionusernumber.setText("" + resCommission.otherData.historyTeam);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter.MViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.mycommissionitem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder (@NonNull Adapter.MViewHolder mViewHolder, int i) {
            if (i % 2 != 0) {
                mViewHolder.mycommissionitem.setBackgroundResource(R.color.blue);
            } else {
                mViewHolder.mycommissionitem.setBackgroundResource(R.color.royalblue);
            }
            mViewHolder.mycommissiontime.setText("" + dataBeans.get(i).belongTime);
            mViewHolder.mycommissionmoney.setText("" + dataBeans.get(i).money);
            mViewHolder.mycommissionnum.setText("" + dataBeans.get(i).count);
            mViewHolder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick (View v) {
                    EventBus.getDefault().post(new ReloginEvent("100"));
                }
            });

        }

        @Override
        public int getItemCount () {
            return dataBeans.size();
        }

        @Override
        public void onItemClick (View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            LinearLayout mycommissionitem;
            TextView mycommissiontime, mycommissionmoney, mycommissionnum, more;

            MViewHolder (View itemView) {
                super(itemView);
                mycommissionitem = (LinearLayout) itemView.findViewById(R.id.mycommissionitem);
                mycommissiontime = (TextView) itemView.findViewById(R.id.mycommissiontime);
                mycommissionmoney = (TextView) itemView.findViewById(R.id.mycommissionmoney1);
                mycommissionnum = (TextView) itemView.findViewById(R.id.mycommissionnum);
                more = (TextView) itemView.findViewById(R.id.more);
            }
        }

    }
}
